﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace Apmis2._1.Models
{
    class PatientRepository : IPatientRepository
    {
        private ApmisContext _ctx;
        public PatientRepository(ApmisContext ctx)
        {
            _ctx = ctx;
        }


        public IQueryable<Patient> GetPatients()
        {
            return _ctx.Patients.Include("RegNos");
            // throw new NotImplementedException();
        }

        public IQueryable<Patient> GetPatientswithregNo(Guid ID)
        {
            return _ctx.Patients
                .Include("RegNos")
                .Where(r =>r.Id==ID);
            // throw new NotImplementedException();
        }






        public Patient GetPatientByID(Guid id)
        {
            return _ctx.Patients.Find(id);
            //throw new NotImplementedException();
        }

      

        public RegNo GetRegNoByID(Guid Id)
        {
            return _ctx.RegNos.Find(Id);
                
        }

        public IQueryable<RegNo> GetRegNoByPatientID(Guid Id)
        {
            return _ctx.RegNos.Where(r=>r.PatientId == Id);
        }


        public bool save()
        {
            //throw new NotImplementedException();
            try
            {

                return _ctx.SaveChanges()>0;
            }
            catch (Exception ex)
            {
                
                //throw- log error;
                return false;
            }
        }

        public bool addPatient(Patient newpatient)
        {
            try
            {
                
               

                var guidID = Guid.NewGuid();
                newpatient.Id = guidID;
                
                _ctx.Patients.Add(newpatient);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw;
            }
        }


        public bool updatePatient(Patient patient)
        {
            throw new NotImplementedException();
        }


        public bool addRegNo(RegNo regno)
        {
            try
            {
                   var guidID = Guid.NewGuid();
                    regno.Id = guidID;
               
                _ctx.RegNos.Add(regno);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw;
            }
        }
    }
}
